package com.web_bloks.content.client;

import java.util.HashMap;

//import com.google.gwt.core.client.GWT;
import com.web_bloks.document.client.Namespace;
import com.web_bloks.document.client.Title;
import com.web_bloks.system.client.Logger;

/**
 * This class helps map a Title to a ContentPage.
 * Wildcards are supported.
 * 
 * Clients of this class should CONFIGURE
 * an instance with the 'add' method.
 * 
 * @author Jean-Lou Dupont
 *
 */
abstract public class ViewManager {

	/**
	 * Response class
	 */
	public final class Response {
		
		ResponseCode rc = null;
		ViewPage  vp = null;
		
		protected Response() {
		}
		
		public ResponseCode getResponseCode() {
			return rc;
		}
		public ViewPage getViewPage() {
			return vp;
		}
	}//END

	/**
	 * ResponseCode class
	 */
	public enum ResponseCode {
		NORMAL, WILD, DEFAULT, NOT_FOUND
	}
	
	/*****************************************
	 * FIELDS 
	 *****************************************/
	static Title WILDTITLE = null; 
	HashMap<String,ViewPage> map = null;
	
	/*****************************************
	 * CONSTRUCTOR 
	 *****************************************/

	protected ViewManager() {
		super();
		
		this.map = new HashMap<String,ViewPage>();
		setup();
	}
	private void setup() {
		WILDTITLE = Title.newWild(); 
	}
	/*****************************************
	 * CONFIGURATION 
	 *****************************************/
	protected void addMapping(ViewPage vPage) {
		//Logger.logDebug(this.getClass()+".addMapping: name="+vpage.getPageName());
		map.put(vPage.getViewName(), vPage);
	}
	
	protected void addMapping( Title title, ViewPage vPage) {
		//Logger.logDebug(this.getClass()+".addMapping: title="+title);
		map.put( title.toString(), vPage );
	}
	/*****************************************
	 * API
	 *****************************************/
	/**
	 * 1st) try matching directly
	 * 2nd) try matching with [ns;_wild_]
	 * 3rd) try matching with default e.g. [_wild_;_wild_] 
	 */
	public Response findMatch(Title title) {

		if (null == title)
			throw new RuntimeException(this.getClass()+".findMatch: title can't be null");
		
		//Logger.logDebug(this.getClass()+".findMatch, title name="+title);
		
		Response rep = new ViewManager.Response();
		rep.rc = ResponseCode.NOT_FOUND;
		
		//1st
		if ( map.containsKey(title.toString()) ) {
			Logger.logInfo(this.getClass()+".findMatch: NORMAL");
			rep.rc = ResponseCode.NORMAL;
			rep.vp = map.get( title.toString() );
			return rep;
		}
			
		Namespace ns = title.getNamespace();
		Title wt = Title.newWild(ns);
		
		//2nd
		if ( map.containsKey(wt.toString()) ) {
			Logger.logInfo(this.getClass()+".findMatch: WILD");			
			rep.rc = ResponseCode.WILD;			
			rep.vp = map.get( wt );
			return rep;
		}
		
		//3rd
		if ( map.containsKey(WILDTITLE.toString()) ) {
			Logger.logInfo(this.getClass()+".findMatch: DEFAULT");			
			rep.rc = ResponseCode.DEFAULT;
			rep.vp = map.get( WILDTITLE.toString() );
			//fall-through
		} else {
			Logger.logInfo(this.getClass()+".findMatch: NOT FOUND");
		}
				
		return rep;
	}
	
	
	
}//END